package org.spring.test.v4;

import java.lang.reflect.Field;
import java.util.LinkedList;

import org.junit.Assert;
import org.junit.Test;
import org.spring.beans.factory.annotation.AutowiredFieldElement;
import org.spring.beans.factory.annotation.InjectionElement;
import org.spring.beans.factory.annotation.InjectionMetadata;
import org.spring.beans.factory.support.DefaultBeanFactory;
import org.spring.beans.factory.xml.XMLBeanDefinationReader;
import org.spring.core.io.ClassPathResource;
import org.spring.dao.v4.AccountDao;
import org.spring.dao.v4.ItemDao;
import org.spring.service.v4.PetStoreService;

public class InjectionMetadataTet {

	@Test
	public void testInjection() throws NoSuchFieldException, SecurityException {
		DefaultBeanFactory beanFactory = new DefaultBeanFactory();
		XMLBeanDefinationReader reader = new XMLBeanDefinationReader(beanFactory);
		reader.loadBeanDefination(new ClassPathResource("petstore-v4.xml"));

		Class<?> clz = PetStoreService.class;
		LinkedList<InjectionElement> elements = new LinkedList<InjectionElement>();

		{
			Field f = PetStoreService.class.getDeclaredField("accountDao");
			InjectionElement injectionElem = new AutowiredFieldElement(f, true, beanFactory);
			elements.add(injectionElem);
		}
		{
			Field f = PetStoreService.class.getDeclaredField("itemDao");
			InjectionElement injectionElem = new AutowiredFieldElement(f, true, beanFactory);
			elements.add(injectionElem);
		}

		InjectionMetadata metadata = new InjectionMetadata(clz, elements);

		PetStoreService petStore = new PetStoreService();

		metadata.inject(petStore);

		Assert.assertTrue(petStore.getAccountDao() instanceof AccountDao);

		Assert.assertTrue(petStore.getItemDao() instanceof ItemDao);
	}

}
